import type { Module } from "vuex";

import type { IRootState } from "../../types";
import type { IDashboard } from "./types";

import {
  getAmountList,
  getCategoryGoodsCount,
  getCategoryGoodsSale,
  getCategoryGoodsFavor,
  getGoodsSaleTop10,
  getGoodsAddressSale,
} from "@/service/main/analysis/dashboard";
import store from "@/store";

const dashboardModule: Module<IDashboard, IRootState> = {
  namespaced: true,
  state() {
    return {
      topPanelDatas: [],
      categoryGoodsCount: [],
      categoryGoodsSale: [],
      categoryGoodsFavor: [],
      goodsSaleTop10: [],
      goodsAddressSale: [],
    };
  },
  mutations: {
    changeGetAmountList(state, list) {
      state.topPanelDatas = list;
    },
    changeCategoryGoodsCount(state, list) {
      state.categoryGoodsCount = list;
    },
    changeCategoryGoodsSale(state, list) {
      state.categoryGoodsSale = list;
    },
    changeCategoryGoodsFavor(state, list) {
      state.categoryGoodsFavor = list;
    },
    changeGoodsSaleTop10(state, list) {
      state.goodsSaleTop10 = list;
    },
    changeGoodsAddressSale(state, list) {
      state.goodsAddressSale = list;
    },
  },
  actions: {
    async categoryGoodsCountActions({ commit }) {
      const amountList = await getAmountList();
      commit("changeGetAmountList", amountList.data);
      
      const categoryGoodsCount = await getCategoryGoodsCount();
      commit("changeCategoryGoodsCount", categoryGoodsCount.data);

      const categoryGoodsSale = await getCategoryGoodsSale();
      commit("changeCategoryGoodsSale", categoryGoodsSale.data);

      const categoryGoodsFavor = await getCategoryGoodsFavor();
      commit("changeCategoryGoodsFavor", categoryGoodsFavor.data);

      const goodsSaleTop10 = await getGoodsSaleTop10();
      commit("changeGoodsSaleTop10", goodsSaleTop10.data);

      const goodsAddressSale = await getGoodsAddressSale();
      commit("changeGoodsAddressSale", goodsAddressSale.data);
    },
  },
};

export default dashboardModule;
